package com.zang.blogz.strategy.context;

import com.zang.blogz.enmus.SearchModeEnum;
import com.zang.blogz.enmus.UploadStrategyEnum;
import com.zang.blogz.exception.BizException;
import com.zang.blogz.model.output.ArticleSearchVO;
import com.zang.blogz.strategy.SearchStrategy;
import io.github.vampireachao.stream.core.optional.Opp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author ZVerify
 * @since 2022/10/11 14:19
 **/
@Service
public class SearchStrategyContext {

    @Value("${search.mode}")
    private String searchMode;

    @Autowired
    private Map<String, SearchStrategy> searchStrategyMap;



    public List<ArticleSearchVO> executeSearchStrategy(String keywords) {

        Opp<List<ArticleSearchVO>> ofTry = Opp.ofTry(() -> searchStrategyMap.get(SearchModeEnum.getStrategy(searchMode)).searchArticle(keywords), NullPointerException.class);

        if (Opp.of(ofTry.getException()).isPresent()) {

            throw new BizException("抱歉,请检查配置文件,当前没有你所选择的搜索策略");
        }
        return ofTry.get();
    }
}
